package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleModule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IvleOtherActivity extends Activity {

	
	/** Called when the activity is first created. */
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.ivleotheramoduleactivitytab);
//
//		lv_arr[0] = "GradeBook";
//		lv_arr[1] = "Class Roster";
//		lv_arr[2] = "Library e-Reserve";
//		lv_arr[3] = "Time Table";
//
//		lv = (ListView) findViewById(R.id.othermoduleLV);
//		lv.setOnItemClickListener(new OnItemClickListener() {
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//					long arg3) {
//				// int option=
//				// Integer.parselv.getItemAtPosition(arg2).toString());
//				// Log.i("value of option",
//				// lv.getItemAtPosition(arg2).toString());//i("value of option is",lv.getItemIdAtPosition(arg2));
//
//				// Log.i("value of option","option"+lv.getItemIdAtPosition(arg2));
//				// int option = (int)lv.getItemIdAtPosition(arg2);
//				switch (arg2) {
//				case 0:
//					startActivity(new Intent("com.nus.IvleGradeActivity"));
//					break;
//				case 1:
//					startActivity(new Intent("com.nus.IvleRosterActivity"));
//					break;
//				case 2:
//					startActivity(new Intent("com.nus.IvleLibraryActivity"));
//					break;
//				case 3:
//					startActivity(new Intent("com.nus.IvleTimeTableActivity"));
//					break;
//				}
//			}
//
//		});
//
//		lv.setAdapter(new ArrayAdapter<String>(this,
//				android.R.layout.simple_list_item_1, lv_arr));
//
//	}
	private ListView lv;
	private String lv_arr[];
	public static String COURSE_ID;
	public static String SEMESTER_ID;
	public static String COURSE_ACAD_YEAR;
	private ArrayList<String> options;
	
	public void onCreate(Bundle savedInstanceState) {

		

                 options = new ArrayList<String>();		
				super.onCreate(savedInstanceState);
				setContentView(R.layout.ivleotheramoduleactivitytab);

//				String url = "https://ivle.nus.edu.sg/api/Lapi.svc/Modules?APIKey=W7RtgW7GZKUpaEkJ1NwUD&AuthToken="
//						+ NUS_Android.IVLE_Token
//						+ "&Duration=0&IncludeAllInfo=false&output=json";
				
				options.add("Grade Book");
				options.add("Class Roster");
				options.add("Library eReserve");
				options.add("TimeTable");

				lv = (ListView) findViewById(R.id.list_view);

				lv.setAdapter(new ListAdapter(IvleOtherActivity.this,
						R.id.other_item, options));
		           OptionsActivity.pd.dismiss();
			}


			private class ListAdapter extends ArrayAdapter<String> { // --
				// CloneChangeRequired
				private ArrayList<String> mList; // --CloneChangeRequired
				private Context mContext;

				public ListAdapter(Context context, int textViewResourceId,
						ArrayList<String> list) { // --CloneChangeRequired
					super(context, textViewResourceId, list);
					this.mList = list;
					this.mContext = context;
				}

				public View getView(int position, View convertView, ViewGroup parent) {
					View view = convertView;
					final int pos=position;
					try {
						if (view == null) {
							LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
							view = vi.inflate(R.layout.ivleotheritem, null);
						}
//						for(int i=0;i<mList.size();i++){
				// Log.i("Other Otions"," value of i is "+i+"arraylist value is"+mList.get(i));
				ImageView iv = (ImageView) view.findViewById(R.id.option_img);
				switch (pos) {
				case 0:
					iv.setImageResource(R.drawable.gradebook);
					break;
				case 1:
					iv.setImageResource(R.drawable.roster1);
					break;
				case 2:
					iv.setImageResource(R.drawable.libraryer);
					break;
				case 3:
					iv.setImageResource(R.drawable.timetable);
					break;

				default:
					break;
						}
						TextView tv = (TextView)view.findViewById(R.id.option);
							tv.setText(mList.get(pos));
							tv.setClickable(true);
							view.setOnClickListener( new OnClickListener() {	
								public void onClick(View v) {
									// TODO Auto-generated method stub
//								 TextView onclick = (TextView)view.findViewById(R.id.option);
								 String data=mList.get(pos);
								 Log.i(" other","value clicked"+data);
									if(data.toLowerCase().equals("grade book")){
										startActivity(new Intent("com.nus.IvleGradeActivity"));
									} else if(data.toLowerCase().equals("class roster")){
										startActivity(new Intent("com.nus.IvleRosterActivity"));
									} else if(data.toLowerCase().equals("library ereserve")){
										 startActivity(new Intent("com.nus.IvleLibraryActivity"));
									} else if(data.toLowerCase().equals("timetable")){
										startActivity(new Intent("com.nus.IvleTimeTableActivity"));
									}
									v.setBackgroundColor(Color.parseColor("#191919"));
								}
							});
		                        view.setOnTouchListener(new OnTouchListener() {
								
								public boolean onTouch(View v, MotionEvent event) {
									// TODO Auto-generated method stub
									v.setBackgroundColor(Color.BLUE);
									return false;
								}
							});
						}
					 catch (Exception e) {
						Log.i(IvleOtherActivity.ListAdapter.class.toString(),
								e.getMessage());
					}
					return view;
				}
			}


	
	}


